<%@ page import="org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils; com.the8thfold.anton.users.Roles" contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>Scripts</title>
    <meta name="layout" content="main" />
    <g:javascript>
        $(document).ready(function(){

        });
    </g:javascript>
</head>
<body>

<div>
    <h1>Scripts</h1>
    <hr/>
    <br/>
    <div class="css_right toolbar">
        <sec:ifAllGranted roles="${Roles.ROLE_SCRIPT_ADMIN.toString()}">
            <a id="newScriptButton" class="btn-text" href="${createLink(controller: 'script', action: 'create')}">Create Script</a>
        </sec:ifAllGranted>
    </div>
    <br/>
    <br/>
    <br/>
    <table>
        %{--<tr>
            <th>Name</th>
            <th>File Name</th>
            <th>Created By</th>
            <th># Jobs</th>
            <th></th>
        </tr>--}%
        <g:each in="${scripts}" status="scriptIndex" var="script">
            <tr class="${scriptIndex%2 ? 'odd' : 'even'}">
                <td>
                    <g:link controller="script" action="view" id="${script.scriptId}">${script.description}</g:link>
                </td>
                %{
                    String fileName = new File(script.scriptFilePath).getName()
                }%
                <td >${fileName}</td>
                <td title="${script.dateCreated}">${script.createdBy.firstName + " " + script.createdBy.lastName} (<prettytime:display date="${script.dateCreated}" />)</td>
                <td>
                    <g:if test="${SpringSecurityUtils.ifAllGranted(Roles.ROLE_JOB_RUN.toString())}">
                        <g:link controller="job" action="list" params="[scriptId: script.scriptId]">${script.jobs.size()}</g:link>
                    </g:if>
                    <g:else>${script.jobs.size()}</g:else>
                </td>
                <td>
                    <div class="toolbar">
                        <sec:ifAllGranted roles="${Roles.ROLE_SCRIPT_ADMIN.toString()}">
                            <sec:ifAllGranted roles="${Roles.ROLE_JOB_ADMIN.toString()}">
                                <a class="btn-text" href="${createLink(controller: 'job', action: 'create', params: [scriptId: script.scriptId])}">Create Job</a>
                            </sec:ifAllGranted>
                            <a class="btn-text" href="${createLink(controller: 'script', action: 'delete', id: script.scriptId)}" onclick="return confirm('Are you sure?');">Delete</a>
                        </sec:ifAllGranted>
                    </div>
                </td>
            </tr>
        </g:each>
    </table>
</div>

</body>
</html>